package com.center.activity;

import android.content.ComponentName;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ClipboardUtils;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.center.R;
import com.center.adapter.ShareAdapter;
import com.center.databinding.ShareActivityBinding;
import com.center.model.ShareModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tencent.connect.share.QQShare;
import com.tencent.connect.share.QzonePublish;
import com.tencent.connect.share.QzoneShare;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import base.QActivity;
import constant.AppConstants;

import static com.tencent.connect.share.QQShare.SHARE_TO_QQ_KEY_TYPE;
import static com.tencent.connect.share.QzonePublish.PUBLISH_TO_QZONE_TYPE_PUBLISHMOOD;
import static com.tencent.connect.share.QzoneShare.SHARE_TO_QZONE_TYPE_IMAGE_TEXT;

public class ShareActivity extends QActivity {
    private static Tencent mTencent;

    private ShareActivityBinding binding;
    private ShareAdapter shareAdapter;

    private ArrayList<ShareModel> list = new ArrayList<>();

    @Override
    public int getLayoutRes() {
        return R.layout.share_activity;
    }

    @Override
    public void initView() {
        mTencent = Tencent.createInstance(AppConstants.APP_ID, this);
        binding = DataBindingUtil.setContentView(this, getLayoutRes());

    }

    @Override
    public void initData() {
        list.add(new ShareModel("微信好友", R.mipmap.wx));
        list.add(new ShareModel("QQ好友", R.mipmap.qq));
        list.add(new ShareModel("QQ空间", R.mipmap.qq));
        list.add(new ShareModel("复制链接", R.mipmap.cop));
        list.add(new ShareModel("取消分享", R.mipmap.black));
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        binding.rvShare.setLayoutManager(layoutManager);
        shareAdapter = new ShareAdapter(R.layout.share_item, list);
        binding.rvShare.setAdapter(shareAdapter);

    }

    @Override
    public void initListener() {
        shareAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                if (position == 0) {
                    shareToFriend();
                } else if (position == 1) {
                    onClickShare();
                } else if (position == 2) {
                    shareToQzone();
                } else if (position == 3) {

                    ClipboardUtils.copyText("分享APP链接: https://www.pgyer.com/nHZx1W");
                    ToastUtils.showLong("复制成功-->分享给好友吧");
                } else if (position == 4) {
                    finish();

                }
            }
        });
    }

    /**
     * 分享信息到微信好友
     */
    private void shareToFriend() {
        Intent intent = new Intent();
        ComponentName componentName = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
        intent.setComponent(componentName);
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("image/*");
        intent.putExtra(Intent.EXTRA_TEXT, "分享APP链接: https://www.pgyer.com/nHZx1W");
        intent.putExtra(Intent.EXTRA_SUBJECT, "分享APP");
        startActivity(intent);
    }


    @Override
    public void networkMessage() {

    }


    //QQ空间
    private void shareToQzone() {
        final Bundle params = new Bundle();
//分享类型
        params.putInt(SHARE_TO_QQ_KEY_TYPE, SHARE_TO_QZONE_TYPE_IMAGE_TEXT);
        params.putString(QzoneShare.SHARE_TO_QQ_TITLE, "Anime");//必填
        params.putString(QzoneShare.SHARE_TO_QQ_SUMMARY, "邀请你来一起看");//选填
        params.putString(QzoneShare.SHARE_TO_QQ_TARGET_URL, "https://www.pgyer.com/nHZx1W");//必填
//        params.putStringArrayList(QzoneShare.SHARE_TO_QQ_IMAGE_URL, "https://www.pgyer.com/nHZx1W");
        mTencent.shareToQzone(this, params, new IUiListener() {
            @Override
            public void onComplete(Object o) {
                LogUtils.d("QQ空间 --> --> onComplete");
            }

            @Override
            public void onError(UiError uiError) {
                LogUtils.d("QQ空间 --> --> onError" + uiError);
            }

            @Override
            public void onCancel() {
                LogUtils.d("QQ空间 --> --> onCancel");
            }

            @Override
            public void onWarning(int i) {
                LogUtils.d("QQ空间 --> --> onWarning");
            }
        });
    }


    //QQ好友
    private void onClickShare() {
        final Bundle params = new Bundle();
//        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare. SHARE_TO_QQ_TYPE_IMAGE);

        params.putInt(SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "Anime");
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "邀请你来一起看");
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "https://www.pgyer.com/nHZx1W");
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "https://www.pgyer.com/nHZx1W");
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, AppUtils.getAppName());
//       params.putInt(QQShare.SHARE_TO_QQ_EXT_INT, "其他附加功能");
        mTencent.shareToQQ(this, params, new IUiListener() {
            @Override
            public void onComplete(Object o) {
                LogUtils.d("QQ好友 --> onComplete");
            }

            @Override
            public void onError(UiError uiError) {
                LogUtils.d("QQ好友 --> --> onError" + uiError);
            }

            @Override
            public void onCancel() {
                LogUtils.d("QQ好友 --> --> onCancel");
            }

            @Override
            public void onWarning(int i) {
                LogUtils.d("QQ好友 --> --> onWarning");
            }
        });
    }
}
